//
//  MineRegisterController.m
//  ArtDog
//
//  Created by dllo on 16/8/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MineRegisterController.h"
#import "CustomTextFiled.h"
#import "MineLoginController.h"
#import "ReminderView.h"
#import "BaseNavigationBar.h"
#import "ZWYNetTool.h"
#import "NSString+Common.h"

@interface MineRegisterController ()

@property (weak, nonatomic) IBOutlet CustomTextFiled *codeText;
@property (weak, nonatomic) IBOutlet CustomTextFiled *phoneNumberText;
@property (weak, nonatomic) IBOutlet CustomTextFiled *passwordText;

@property (weak, nonatomic) IBOutlet BaseNavigationBar *navigationBar;

@property (weak, nonatomic) IBOutlet UIButton *readedBtn;
@property (weak, nonatomic) IBOutlet UILabel *readedLabel;
@property (weak, nonatomic) IBOutlet UIButton *protocolBtn;
@property (weak, nonatomic) IBOutlet UIButton *loginAtOnce;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;

@end

@implementation MineRegisterController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nil bundle:nibBundleOrNil];
    if (self) {
        _isForgetVC = NO;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (_isForgetVC == YES) {
        _passwordText.placeholder = @"请输入注册时的手机号";
        _passwordText.placeholder = @"重置密码(6位以上的字母或数字)";
       _navigationBar.items[0].title = @"重置密码";
        _readedBtn.hidden = YES;
        _readedLabel.hidden = YES;
        _protocolBtn.hidden = YES;
        _loginAtOnce.hidden = YES;
        [_nextBtn setTitle:@"完成" forState:UIControlStateNormal];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isFreeKeyboard = YES;
    _phoneNumberText.placeholder = @"手机号";
    _codeText.placeholder = @"验证码";
    [_codeText.rightBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    _codeText.rightImageView.image = [UIImage imageNamed:@"icon_phone"];
    _passwordText.placeholder = @"请输入6位以上的字母或数字作为密码";
    _readedBtn.layer.borderWidth = 1;
    _readedBtn.layer.borderColor = [UIColor blackColor].CGColor;
    [_codeText.rightBtn addTarget:self action:@selector(getCode:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)getCode:(UIButton *)sender
{
    if (_phoneNumberText.textField.text.length != 11) {
        [ReminderView reminderWithSuperView:_codeText andTitle:@"请输入11位有效的手机号!" andPosition:HLAlertViewPositionDown];
    } else {
        NSString *urlString = [@"http://api.artgoer.cn:8084/artgoer/api/v1/sendSms?mobileNo=" stringByAppendingString:_phoneNumberText.textField.text];
        [ZWYNetTool GET:urlString andBody:nil andHeader:@{@"Host": @"api.artgoer.cn:8084"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
            [ReminderView reminderWithSuperView:_codeText andTitle:result[@"message"] andPosition:HLAlertViewPositionDown];
            
        } andFailureBlock:^(NSError *error) {
            NSLog(@"%@", error);
        }];
    }
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)nextAction:(id)sender {
    if (_phoneNumberText.textField.text.length != 11) {
        [ReminderView reminderWithSuperView:_codeText andTitle:@"请输入11位有效的手机号!" andPosition:HLAlertViewPositionDown];
    } else if (_codeText.textField.text.length == 0) {
        [ReminderView reminderWithSuperView:_codeText andTitle:@"请输入验证码!" andPosition:HLAlertViewPositionDown];
    } else if (_passwordText.textField.text.length == 0) {
        [ReminderView reminderWithSuperView:_codeText andTitle:@"请输入密码!" andPosition:HLAlertViewPositionDown];
    } else if ((_passwordText.textField.text.length > 0 && _passwordText.textField.text.length < 6) || _passwordText.textField.text.length > 20) {
        [ReminderView reminderWithSuperView:_codeText andTitle:@"密码最小长度为6~20位!" andPosition:HLAlertViewPositionDown];
    } else {
        NSString *urlString = [@"http://api.artgoer.cn:8084/artgoer/api/v1/regist?" stringByAppendingString:_phoneNumberText.textField.text];
        NSString *paramString = [[[[[@"mobileNo=" stringByAppendingString:_phoneNumberText.textField.text]stringByAppendingString:@"&code="]stringByAppendingString:[_codeText.textField.text stringWith32BitMD5Lower]]stringByAppendingString:@"&password="]stringByAppendingString:[_passwordText.textField.text stringWith32BitMD5Lower]];
        [ZWYNetTool POST:urlString andBody:paramString andBodyStyle:ZWYBodyString andHeader:@{@"Host": @"api.artgoer.cn:8084"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
            [ReminderView reminderWithSuperView:_codeText andTitle:result[@"message"] andPosition:HLAlertViewPositionDown];
            NSLog(@"%@", result);
            if ([result[@"status"] isEqualToString:@"200"]) {
                NSLog(@"注册成功");
            }
        } andFailureBlock:^(NSError *error) {
            NSLog(@"%@", error);
        }];
    }
    
}

- (IBAction)loginAction:(id)sender {
    MineLoginController *loginVC = [[MineLoginController alloc]initWithNibName:@"MineLoginController" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:loginVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
